package com.moorror.codegenvel.tab;

import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
import com.intellij.openapi.project.Project;
import com.intellij.util.ThrowableRunnable;
import com.moorror.codegenvel.utils.FileProviderFactoryEditorUtils;

import javax.swing.*;

/**
 * @Author jiyanghuang
 * @Date 2025/8/17 19:32
 */
public class TabWrapEditor extends MRTabWrap {

    private final EditorFactory factory = EditorFactory.getInstance();
    private final EditorHighlighterFactory highlighterFactory = EditorHighlighterFactory.getInstance();

    /**
     * 编辑文档
     */
    private Editor editor;

    public TabWrapEditor(Project project, int index, String title, JTabbedPane tabbedPane, String jsonContent, boolean jbScrollPaneWrap) {
        super(project, index, title, tabbedPane, null, jbScrollPaneWrap);
        this.editor = createEditor(jsonContent, "vm");
        updateJComponent(this.editor.getComponent(), false);
        fillSource(jsonContent);
    }

    public TabWrapEditor(Project project, int index, String title, JTabbedPane tabbedPane, String extension, String jsonContent, boolean jbScrollPaneWrap) {
        super(project, index, title, tabbedPane, null, jbScrollPaneWrap);
        this.editor = createEditor(jsonContent, extension);
        updateJComponent(this.editor.getComponent(), false);
        fillSource(jsonContent);
    }

    public void fillSource(String source) {
        if (null == source) {
            source = "";
        }
        final String sourceLast = source;
        try {
            WriteAction.run(new ThrowableRunnable<Throwable>() {
                @Override
                public void run() throws Throwable {
                    editor.getDocument().setText(sourceLast);
                }
            });
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    public String text() {
        return this.editor.getDocument().getText();
    }

    private Editor emptyEditor() {
        return createEditor("", "vm");
    }

    /**
     * 创建编辑器
     */
    private Editor createEditor(String template, String extension) {
        if(null == template){
            template = "";
        }
        Document velocityTemplate = factory.createDocument(template);
        Editor editor = factory.createEditor(velocityTemplate,
                null, FileProviderFactoryEditorUtils.getFileType(extension), false);

        EditorSettings editorSettings = editor.getSettings();
        editorSettings.setLineNumbersShown(true);
        return editor;
    }

    public static Editor createEditorStatic(String template, String extension) {
        EditorFactory factory = EditorFactory.getInstance();
        if(null == template){
            template = "";
        }
        Document velocityTemplate = factory.createDocument(template);
        Editor editor = factory.createEditor(velocityTemplate,
                null, FileProviderFactoryEditorUtils.getFileType(extension), false);

        EditorSettings editorSettings = editor.getSettings();
        editorSettings.setLineNumbersShown(true);
        return editor;
    }
}
